﻿using Dapper;
using Himp.TaskScheduling.Hangfire.Models;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using System.Data;
using System.Text;

namespace Himp.TaskScheduling.Hangfire.Jobs
{
    /// <summary>
    /// 机器人_灰分化验结果
    /// </summary>
    public class RILS1810ViewAJob : CollectTestDataJob, IHostedService
    {
        #region SQL

        public const string GET_RILS1810ViewA_SQL = @"
            SELECT *
            FROM [dbo].[View_A]
            WHERE CONVERT(VARCHAR(10), [EndDate], 120) = CONVERT(VARCHAR(10), GETDATE(), 120)
            ";

        #endregion

        #region 字段

        private readonly ConnectionStringOption _connectionStrings;
        private ILogger<RILS1810ViewAJob> _logger;

        #endregion

        #region 方法

        public RILS1810ViewAJob(ILogger<RILS1810ViewAJob> logger
            , IOptions<ConnectionStringOption> connectionStrings) : base(logger, connectionStrings)
        {
            _logger = logger;
            _connectionStrings = connectionStrings.Value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("--------启动:机器人化验_灰分结果--------");

            try
            {
                using (IDbConnection connection = new SqlConnection(_connectionStrings.KaiY_RILS1810))
                {
                    connection.Open();
                    // 读取数据
                    var aRecList = connection.Query<RILS1810ViewA>(GET_RILS1810ViewA_SQL);
                    var testProjectID = "300";

                    foreach (var item in aRecList)
                    {
                        var existTestCode = ExistTestCode(item.SampleID);
                        var thirdCode = item.SampleID;

                        if (!existTestCode)
                        {
                            continue;
                        }

                        //var thirdCode = BottomCodeConvertTestCode(item.SampleID);

                        //if (string.IsNullOrEmpty(thirdCode))
                        //{
                        //    _logger.LogError($"瓶底码{item.SampleID}:化验码为空");
                        //    continue;
                        //}

                        CollectTestDataModel collectTestDataModel = new CollectTestDataModel();
                        collectTestDataModel.thirdCode = thirdCode;

                        if (collectTestDataModel.thirdCode.Length > 11)
                        {
                            collectTestDataModel.thirdCode = collectTestDataModel.thirdCode.Substring(collectTestDataModel.thirdCode.Length - 11);
                        }

                        collectTestDataModel.gh = item.CurrentUser;
                        collectTestDataModel.peopleName = item.CurrentUser;
                        collectTestDataModel.testTime = item.EndDate.ToString("yyyy-MM-dd HH:mm:ss");
                        collectTestDataModel.originalRecordNo = $"RILS1810_A_{item.ID}_{testProjectID}";
                        collectTestDataModel.labID = "1";
                        collectTestDataModel.testProjectID = testProjectID;
                        collectTestDataModel.testDeviceID = "1";
                        collectTestDataModel.unitID = "0";

                        StringBuilder sbTestZBID = new StringBuilder();
                        StringBuilder sbTestValue = new StringBuilder();
                        StringBuilder sbTestDevice = new StringBuilder();

                        if (item.Aad != 0)
                        {
                            sbTestZBID.Append(",308");
                            sbTestValue.Append($",{item.Aad}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.CruWeight != 0) //灰皿重
                        {
                            sbTestZBID.Append(",302");
                            sbTestValue.Append($",{item.CruWeight}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.SampleWeight != 0) //灰分煤样重
                        {
                            sbTestZBID.Append(",303");
                            sbTestValue.Append($",{item.SampleWeight}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.SampleWeight != 0 && item.CruWeight != 0) //总样质量(g)
                        {
                            sbTestZBID.Append(",304");
                            sbTestValue.Append($",{item.SampleWeight+ item.CruWeight}");
                            sbTestDevice.Append(",11");
                        }

                        if (item.SampleCalRemain != 0) //灼烧后总质量(g)
                        {
                            sbTestZBID.Append(",305");
                            sbTestValue.Append($",{item.SampleCalRemain}");
                            sbTestDevice.Append(",11");
                        }

                        if (item.SampleCalRemainW1 != 0) //1次检查性试验后重(g)
                        {
                            sbTestZBID.Append(",306");
                            sbTestValue.Append($",{item.SampleCalRemainW1}");
                            sbTestDevice.Append(",11");
                        }

                        if (item.SampleCalRemainW2 != 0) //2次检查性试验后重(g)
                        {
                            sbTestZBID.Append(",307");
                            sbTestValue.Append($",{item.SampleCalRemainW2}");
                            sbTestDevice.Append(",11");
                        }

                        collectTestDataModel.testZBIDs = sbTestZBID.ToString().Length > 0 ? sbTestZBID.ToString().Substring(1) : string.Empty;
                        collectTestDataModel.testValues = sbTestValue.ToString().Length > 0 ? sbTestValue.ToString().Substring(1) : string.Empty;
                        collectTestDataModel.testDeviceID = sbTestDevice.ToString().Length > 0 ? sbTestDevice.ToString().Substring(1) : string.Empty;

                        Exec(collectTestDataModel);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(exception: e, "异常:机器人化验_灰分结果");
            }

            _logger.LogInformation("--------完成:机器人化验_灰分结果--------");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }

        #endregion
    }
}
